<?php
namespace app\index\controller;

use think\Controller;
use think\Session;

class TodoConfig extends Controller
{
    public function _initialize()
    {
        $id         =   session('user');

        if(!$id)
        {
            $this->redirect('login/login/index');
        }
    }

    /**
     * 配置信息列表
     * @author 秋水
     * @DateTime 2018-12-11T15:28:22+0800
     */
    public function index()
    {
    	$data_list = db('todo_config')->where(['type'=>1])->select();

    	$this->assign('data_list',$data_list);
    	return $this->fetch();
    }

    /**
     * 开关配置信息列表
     * @author 秋水
     * @DateTime 2018-12-13T15:48:07+0800
     */
    public function switchList()
    {
    	$data_list = db('todo_config')->where(['type'=>2])->select();

    	$this->assign('data_list',$data_list);
    	return $this->fetch();
    }

    /**
     * 关闭配置
     * @author 秋水
     * @DateTime 2018-12-13T17:07:52+0800
     */
    public function close()
    {
    	$id = input('id', 0);

    	$r = db('todo_config')->where(['id'=>$id])->update(['value'=>0]);
    	if($r!==false) {
    		return json(['code'=>1,'msg'=>'关闭成功']);
    	} else {
    		return json(['code'=>0,'msg'=>'关闭失败']);
    	}
    }
    /**
     * 开启配置
     * @author 秋水
     * @DateTime 2018-12-13T17:07:52+0800
     */
    public function open()
    {
    	$id = input('id', 0);

    	$r = db('todo_config')->where(['id'=>$id])->update(['value'=>1]);
    	if($r!==false) {
    		return json(['code'=>1,'msg'=>'开启成功']);
    	} else {
    		return json(['code'=>0,'msg'=>'开启失败']);
    	}
    }

    /**
     * 添加配置信息
     * @author 秋水
     * @DateTime 2018-12-12T11:51:53+0800
     */
    public function add()
    {
        if(request()->isPost())
        {
            $data                   =   input();

            $insert = ['name'=>$data['name'],'title'=>$data['title'],'value'=>$data['value']];

            if(db('todo_config')->insert($insert))
            {
                Session::flash('code','2');
                $this->redirect('todo_config/index');
            }else{
                Session::flash('code','err2');
                $this->redirect('todo_config/add');
            };
        }else{
            return view('add');
        }
    }

    /**
     * 修改配置信息
     * @author 秋水
     * @DateTime 2018-12-11T15:29:02+0800
     */
    public function update()
    {
		if(request()->isPost())
        {
            $data                   =   input();

            $insert = ['name'=>$data['name'],'title'=>$data['title'],'value'=>$data['value']];

            if(db('todo_config')->where('id',$data['id'])->update($insert))
            {
                Session::flash('code','2');
                $this->redirect('todo_config/index');
            }else{
                Session::flash('code','err2');
                $this->redirect('todo_config/update', ['id'=>$data['id']]);
            };
        }else{
            $data   =   db('todo_config')->where('id',input('id'))->find();

            return view('update',[
                'page'  =>  input('page','0'),
                'data'  =>  $data,
                'code'  =>  1,
                'msg'   =>  ''
            ]);
        }
    }

    /**
     * 删除配置项
     * @author 秋水
     * @DateTime 2018-12-29T13:09:05+0800
     */
    public function delete()
    {
        $id = input('id',0);
        $r = db('todo_config')->where(['id'=>$id])->delete();

        if($r) {
            return json(['code'=>1,'msg'=>'删除成功']);
        } else {
            return json(['code'=>0,'msg'=>'删除失败']);
        }
    }

    /**
     * 客服信息
     * @author 秋水
     * @DateTime 2018-12-29T20:16:39+0800
     */
    public function serverInfo()
    {
        $serverImg = db('todo_config')->where(['title'=>'server_img'])->find();
        $serverWx = db('todo_config')->where(['title'=>'server_wx'])->find();

        return view('server_info',[
            'server_img'=>$serverImg?$serverImg['value']:'',
            'server_wx'=>$serverWx?$serverWx['value']:'',
        ]);
    }

    /**
     * 修改客服信息
     * @author 秋水
     * @DateTime 2018-12-29T20:20:02+0800
     */
    public function updateServer()
    {
        // $serverImgValue = input('server_img', '');
        $serverWxValue = input('server_wx', '');
        $file = request()->file('server_img');
        if($file){

            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $type   =   ['gif','jpeg','png','bmp','jpg'];
                $types  =   $info->getExtension();
                $url    =   '/public/uploads/'. $info->getSaveName();
                if(in_array($types,$type))
                {
                    $serverImgValue   =   str_replace('\\','/',str_replace('\\\\','/',$url));
                }else{
                    unlink($url);
                    return redirect('todo_config/serverInfo',['code'=>0,'msg'=>'请上传图片']);
                }
            }else{

                return redirect('todo_config/serverInfo',['code'=>0,'msg'=>'上传失败'.$file->getError()]);
            }
        }

        $serverImg = db('todo_config')->where(['title'=>'server_img'])->find();
        if(!$serverImg) {
            db('todo_config')->insert(['type'=>3,'name'=>'客服微二维码','title'=>'server_img','value'=>$serverImgValue]);
        } else {
            db('todo_config')->where(['title'=>'server_img'])->update(['value'=>$serverImgValue]);
        }
        $serverWx = db('todo_config')->where(['title'=>'server_wx'])->find();
        if(!$serverWx) {
            db('todo_config')->insert(['type'=>3,'name'=>'客服微信号','title'=>'server_wx','value'=>$serverWxValue]);
        } else {
            db('todo_config')->where(['title'=>'server_wx'])->update(['value'=>$serverWxValue]);
        }

        return redirect('todo_config/serverInfo');
    }
}